/*
 * // Bristleback plugin - Copyright (c) 2010 bristleback.googlecode.com
 * // ---------------------------------------------------------------------------
 * // This program is free software; you can redistribute it and/or modify it
 * // under the terms of the GNU Lesser General Public License as published by the
 * // Free Software Foundation; either version 3 of the License, or (at your
 * // option) any later version.
 * // This library is distributed in the hope that it will be useful,
 * // but without any warranty; without even the implied warranty of merchantability
 * // or fitness for a particular purpose.
 * // You should have received a copy of the GNU Lesser General Public License along
 * // with this program; if not, see <http://www.gnu.org/licenses/lgpl.html>.
 * // ---------------------------------------------------------------------------
 */

package pl.bristleback.server.bristle.token;

import org.apache.log4j.Logger;
import pl.bristleback.server.bristle.config.BristleConfiguration;
import pl.bristleback.server.bristle.utils.ActionClassElement;

import java.lang.reflect.Field;

/**
 * Creates a tokenizer wrapper containing instance of tokenizer
 * and information for faster tokenizer injection.
 * Class owning that tokenizer wrapper and tokenizer field must be supplied
 * before {@link TokenizerCreator#createTokenizer(BristleConfiguration)} method is invoked.
 * Note that this class does not operate on instance of owner class,
 * it does not inject tokenizer field to owner class.
 * <p/>
 * Created on: 2010-12-16 15:19:29 <br/>
 *
 * @author Wojciech Niemiec
 */
public class TokenizerCreator {
  private static Logger log = Logger.getLogger(TokenizerCreator.class.getName());

  private TokenizeAnnotationProcessor tokenizeAnnotationProcessor = new TokenizeAnnotationProcessor();

  private ActionClassElement<Tokenizer> tokenizerWrapper;
  private Class ownerClass;
  private Field tokenizerField;

  /**
   * Basic constructor, owner class and tokenizer field must be specified separately.
   */
  public TokenizerCreator() {
  }

  /**
   * Constructor used when one tokenizer creator is used to retrieve all tokenizer wrappers.
   *
   * @param ownerClass class containing tokenizer fields.
   */
  public TokenizerCreator(Class ownerClass) {
    this.ownerClass = ownerClass;
  }

  /**
   * Creates a ready to use tokenizer wrapper creator.
   *
   * @param ownerClass       class containing tokenizer fields.
   * @param tokenizerField tokenizer field.
   */
  public TokenizerCreator(Class ownerClass, Field tokenizerField) {
    this.ownerClass = ownerClass;
    this.tokenizerField = tokenizerField;
  }

  /**
   * Creates a tokenizer wrapper.
   *
   * @param configuration Bristleback plugin configuration.
   * @return tokenizer wrapper.
   */
  public ActionClassElement<Tokenizer> createTokenizer(BristleConfiguration configuration) {
    tokenizerWrapper = new ActionClassElement<Tokenizer>();
    Tokenizer tokenizer = new Tokenizer();
    tokenizeAnnotationProcessor.processTokenizerAnnotations(tokenizerField, tokenizer, configuration);
    tokenizerWrapper.setElementValue(tokenizer);
    tokenizerWrapper.setOwnerClass(ownerClass);
    tokenizerWrapper.setElementField(tokenizerField);

    return tokenizerWrapper;
  }

  public void setOwnerClass(Class ownerClass) {
    this.ownerClass = ownerClass;
  }

  public void setTokenizerField(Field tokenizerField) {
    this.tokenizerField = tokenizerField;
  }
}
